package com.qin.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.qin.dao.BookDao;
import com.qin.entity.Book;
import com.qin.entity.BookCategory;
import com.qin.service.BookCategoryService;
import com.qin.service.BookService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.log.StaticLog;

public class BookServiceImpl implements BookService {

	private BookDao bookDao = new BookDao();
	private BookCategoryService bookCategoryService = new BookCategoryServiceImpl();

	@Override
	public Long insertBook(Book book) {
		return bookDao.insertBook(book);
	}

	@Override
	public int deleteBook(Long id) {
		return bookDao.deleteBook(id);
	}

	@Override
	public int updateBook(Long id, Book book) {
		return bookDao.updateBook(id, book);
	}

	@Override
	public Book getBookById(Long id) {
		return bookDao.querryBookById(id);
	}

	@Override
	public List<Book> getAllBook() {
		return bookDao.getAllBooks();
	}

	@Override
	public List<Book> getMasterWork() {
		return bookDao.getMasterWork();
	}

	@Override
	public List<Book> getRecommendBooks(Integer n) {
		return bookDao.getRecommendBooks(n);
	}

	@Override
	public List<Book> getBooksByCategoryLevel1Id(Long id) {
		// 准备一个List，存放查到的所有内容
		List<Book> books = new ArrayList<>();

		// 先根据这个id得到他的所有二级分类
		List<BookCategory> level2 = bookCategoryService.queryBookCategoryByPId(id);

		// 如果二级分类不为空，去查询每一个二级分类的三级分类
		if (level2 != null) {

			Iterator<BookCategory> iterator = level2.iterator();
			while (iterator.hasNext()) {
				// 拿到二级分类的id
				Long id2 = iterator.next().getId();
				StaticLog.debug("通过一级分类找到的二级分类的id：{}", id2);

				// 继续查询,得到三级分类
				List<BookCategory> level3 = bookCategoryService.queryBookCategoryByPId(id2);

				// 通过三级分类的id查询对应的图书
				if (level3 != null) {
					for (BookCategory bookCategory : level3) {
						List<Book> booksByCategoryId = bookDao.getBooksByCategoryId(bookCategory.getId());
						StaticLog.debug("通过1级分类找到的三级分类：{}", booksByCategoryId);
						// 放入图书中
						books.addAll(booksByCategoryId);
					}
				}
			}
		}

		if (books.size() == 0) {
			return null;
		} else {
			StaticLog.debug("通过一个1级分类找到他的三级分类，根据三级分类找到所有关联的图书：{}", books);
			return books;
		}
	}

	@Override
	public List<Book> getBooksByCategoryLevel1IdAndD(Long id, long i) {
		List<Book> booksByCategoryLevel1Id = getBooksByCategoryLevel1Id(id);
		
		if(booksByCategoryLevel1Id != null) {
			List<Book> collect = booksByCategoryLevel1Id.stream().filter(book->{
				return DateUtil.betweenDay(book.getCreateTime(), DateUtil.date(), false) < i;
			}).collect(Collectors.toList());
			
			return collect;
		}
		
		return null;
	}
	
	@Override
	public List<Book> getBooksByCategoryLevel1IdAndD(Long id) {
		return getBooksByCategoryLevel1IdAndD(id, 1L);
	}

	@Override
	public List<Book> getBooksByCategoryLevel2Id(Long id) {
		// 根据这个（二级分类）id查询他的子类（三级分类）
		List<BookCategory> queryBookCategoryByPId = bookCategoryService.queryBookCategoryByPId(id);
		
		if(queryBookCategoryByPId != null) {
			// 遍历他的三级分类，通过id查询图书
			List<Book> collect = queryBookCategoryByPId.stream().flatMap(bookCategory->{
				// 拿到id
				Long categoryId = bookCategory.getId();
				// 查询所有的图书
				List<Book> booksByCategoryId = bookDao.getBooksByCategoryId(categoryId);
				
				if(booksByCategoryId != null) {
					return booksByCategoryId.stream();
				}else {
					return null;
				}
			}).collect(Collectors.toList());
			
			return collect;
		}
		
		return null;
	}

	
	@Override
	public List<Book> getBooksByCategoryLevel3Id(Long id) {
		return bookDao.getBooksByCategoryId(id);
	}
	
}
